package org.aeon.newsmanagersystem.controller;

import com.alibaba.fastjson.JSON;
import org.aeon.newsmanagersystem.annotation.Protected;
import org.aeon.newsmanagersystem.pojo.Topic;
import org.aeon.newsmanagersystem.pojo.other.Return;
import org.aeon.newsmanagersystem.service.NewsService;
import org.aeon.newsmanagersystem.service.TopicService;
import org.aeon.newsmanagersystem.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author: MissingAeon
 * @date: 2024/3/27 17:29
 * @description:
 */
@Controller
@RequestMapping("/topic")
public class TopicController {
    private final TopicService topicService;
    private final NewsService newsService;

    @Autowired
    public TopicController(TopicService topicService, NewsService newsService) {
        this.topicService = topicService;
        this.newsService = newsService;
    }

    @GetMapping("/list")
    public String list(Model model) {
        List<Topic> topics = topicService.display();
        model.addAttribute("topics", topics);
        return "admin/topic_list";
    }

    @GetMapping(value = "/list/data", produces = "text/html;charset=utf-8")
    @ResponseBody
    public String listData() {
        List<Topic> topics = topicService.display();
        return JSON.toJSONString(Return.OK(topics));
        // return "你好";
    }

    @Protected
    @PostMapping("/add")
    public String add(Topic topic, RedirectAttributes attr) {
        if (topic == null || !StringUtils.hasReally(topic.getTname())) {
            attr.addFlashAttribute("result", "主题名称不合法！");
        } else if (topicService.exists(topic)) {
            attr.addFlashAttribute("result", "主题名称已存在！");
        } else {
            final int result = topicService.add(topic);
            if (result == 1) {
                attr.addFlashAttribute("result", "添加成功");
            } else {
                attr.addFlashAttribute("result", "添加失败！");
            }
        }
        return "redirect:/topic/toAdd";
    }

    @Protected
    @GetMapping("/toModify")
    public String toModify(Topic topic, Model model) {
        final Topic found = topicService.get(topic.getTid());
        model.addAttribute("topic", found);
        return "admin/topic_modify";
    }

    @Protected
    @PostMapping("/modify")
    public String modify(Topic topic, Model model, RedirectAttributes attr) {
        if (topic == null || !StringUtils.hasReally(topic.getTname())) {
            model.addAttribute("result", "主题名称不合法！");
        } else if (topicService.exists(topic)) {
            model.addAttribute("result", "主题名称已存在！");
        } else {
            final int result = topicService.modify(topic);
            if (result == 1) {
                attr.addFlashAttribute("result", "修改成功");
                return "redirect:/topic/list";
            } else {
                model.addAttribute("result", "修改失败！");
            }
        }
        model.addAttribute("topic", topic);
        return "admin/topic_modify";
    }

    @Protected
    @GetMapping("/delete")
    public String delete(Integer tid, RedirectAttributes attr) {
        final Topic topic = new Topic();
        topic.setTid(tid);
        if (newsService.exists(topic)) {
            attr.addFlashAttribute("result", "该主题有新闻，无法删除！");
        } else {
            final int result = topicService.delete(tid);
            if (result == 1) {
                attr.addFlashAttribute("result", "删除成功！");
            } else {
                attr.addFlashAttribute("result", "删除失败！");
            }
        }
        return "redirect:/topic/list";
    }

}
